package ModelLayer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collection;


/**
 * Write a description of class StaffMemberCollection here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CustomerCollection
{

       private static HashMap<String, Group> customer_groups;
       private static ArrayList<Customer> customers;
       private static FileCustomer file;
       private static FileGroup groupfile;
       private static CustomerCollection instance;
       
    /**
     * Constructor for objects of class StaffMemberCollection
     */
    private CustomerCollection()
    {
        customers = new ArrayList<>();
        customer_groups = new HashMap<>();
        file = new FileCustomer();
        groupfile = new FileGroup();
        customers = file.get_data();
        customer_groups = groupfile.get_data();
    }
    
    public static void initialize()
    {
        if(instance == null)
        {
            instance = new CustomerCollection();
        }
    }
    
    public static ArrayList<Customer> get_Customers()
    {
        return customers;
    }
    
    public static HashMap<String, Group> get_customer_groups()
    {
        return customer_groups;
    }
    
    public static CustomerCollection get_instance()
    {
        return instance;
    }
    
    public static String get_next_cust_nr()
    {
    	 int max_temp_cust_nr = 0;
         int temp_cust_nr = 0;
          for (Customer customer : customers)
         {
             temp_cust_nr = Integer.parseInt(customer.get_id());
             if(max_temp_cust_nr < temp_cust_nr)
             {
                 if(max_temp_cust_nr < (temp_cust_nr - 1))
                 {
                     if(!(existing_customer(String.valueOf(max_temp_cust_nr + 1))))
                     {
                         break;
                     }
                 }
                 max_temp_cust_nr = temp_cust_nr;     
             }
         }
         return String.valueOf(max_temp_cust_nr + 1);    
    }
    
    public static String add_customer(String name, String phone_nr, String address, String postal_code, String city, String group)
    {
        ArrayList<String> lease_ids = new ArrayList<>();
        ArrayList<String> order_ids = new ArrayList<>();
        int max_temp_id = 0;
        int temp_id = 0;
        String id;
        for (Customer member : customers)
        {
            temp_id = Integer.parseInt(member.get_id());
            if(max_temp_id < temp_id)
            {
                if(max_temp_id < (temp_id - 1))
                {
                    if(!(existing_customer(id = "" + (max_temp_id + 1))))
                    {
                        break;
                    }
                }
                max_temp_id = temp_id;     
            }
        }
        id = "" + (max_temp_id + 1);
        customers.add(new Customer(id, name, phone_nr, address, postal_code, city, group, lease_ids, order_ids));
        file.save_changes(customers);
        return id;
    }
    
    public static boolean existing_customer(String id)
    {
        for (Customer member : customers)
        {
            if(member.get_id().equals(id))
            {
                return true;
            }
        }
        return false; 
    }
    
    public static Customer find_customer(String id)
    {
        for (Customer member : customers)
        {
            if(member.get_id().equals(id))
            {
                return member;
            }
        }
        return null;
    }
    
    public static boolean delete_customer(String id)
    {
        return customers.remove(find_customer(id));
    }
    
    public static boolean update_customer(String id, String name, String phone_nr, String address, String postal_code, String city, String group)
    {
       if (name != "")
       {
           find_customer(id).set_name(name);
       }
       if (phone_nr != "")
       {
           find_customer(id).set_phone_nr(phone_nr);
       }
       if (address != "")
       {
           find_customer(id).set_address(address);
       }
       if (postal_code != "")
       {
           find_customer(id).set_postal_code(postal_code);
       }
       if (city != "")
       {
           find_customer(id).set_city(city);
       }
       if (group != "")
       {
           find_customer(id).set_group(group);
       }
       return file.save_changes(customers);
    }
    
    public static void add_order_to_customer(String id, String order_id)
    {
        find_customer(id).add_order(order_id);
    }
    
    public static void add_lease_to_customer(String id, String lease_id)
    {
        find_customer(id).add_lease(lease_id);
    }
    public static String get_customer_lease(String lease_id)
    {
    	for (Customer customer:customers)
    	{
    		ArrayList<String> temp = customer.get_lease_ids();
    		if (temp.contains(lease_id))
    		{
    			return customer.get_id();
    		}
    	}
    	return null;
    }
        
    
    public static boolean is_group(String name)
    {
        return customer_groups.containsKey(name); 
    }
    
    public static boolean add_group(String name, String terms, double discount)
    {
        customer_groups.put(name,new Group(name,terms,discount));
        return groupfile.save_changes(customer_groups);
    }
    
    public static boolean update_group(String name, String terms, double discount)
    {
        customer_groups.get(name).set_terms(terms);
        customer_groups.get(name).set_discount(discount);
        return groupfile.save_changes(customer_groups);
    }
    
    public static boolean delete_group(String name)
    {
        customer_groups.remove(name);
        return groupfile.save_changes(customer_groups);
    }
    
    public static String view_group(String name)
    {
        return customer_groups.get(name).printable();
    }
    
    public static boolean is_group_used(String name)
    {
        for (Customer member : customers)
        {
            if (member.get_group().equals(name))
            {
                return true;
            }
        }
        return false;
    }
    
    public static double get_group_discount(String id)
    {
        String group_name = find_customer(id).get_group();
        return customer_groups.get(group_name).get_discount();
    }
    
    public static void save_all_data()
    {
        if ((file.save_changes(customers) == groupfile.save_changes(customer_groups)) && (groupfile.save_changes(customer_groups)));
    }
    public static Object[][] display_all_customers()
    {
    	Object[][] all_products = new Object[customers.size()][5];
    	int i = 0;
    	for (Customer articule : customers)
    	{
	    		all_products[i] = articule.give_array();
	    		i++;
    	}
    	return all_products;
    }
    
    public static HashMap<String, String> give_names_and_ids()
    {
    	HashMap<String, String> names_and_ids = new HashMap<>();
    	for (Customer customer : customers)
    	{
    		names_and_ids.put(customer.get_name(), customer.get_id());
    	}
    	return names_and_ids;
    }
    
    public static Object[][] give_all_paid_order_data()
    {
    	ArrayList<Object[]> data = new ArrayList<>();
    	
    	for (Customer customer : customers)
    	{
    		data.addAll(customer.give_paid_order_array());
    	}
    	Object[][] returner = new Object[data.size()][3];
    	int i = 0;
    	for(Object[] instance : data)
    	{
    		returner[i] = instance;
    		i++;
    	}
    	return returner;
    }
    
    public static Object[][] give_all_unpaid_order_data()
    {
    	ArrayList<Object[]> data = new ArrayList<>();
    	
    	for (Customer customer : customers)
    	{
    		data.addAll(customer.give_unpaid_order_array());
    	}
    	Object[][] returner = new Object[data.size()][4];
    	int i = 0;
    	for(Object[] instance : data)
    	{
    		returner[i] = instance;
    		i++;
    	}
    	return returner;
    }
    
    public static Object[] get_order_data(String id, boolean finished)
    {
    	Object[] data = new Object[4];
    	for (Customer customer : customers)
    	{
    		
    		data = customer.find_order(id, finished);
    		
    		if (data != null)
			{
				return data;
			}
    	}
    	return new Object[3];
    }
    
    public static Object[] get_any_order_data(String id)
    {
    	Object[] data = new Object[4];
    	for (Customer customer : customers)
    	{
    		
    		data = customer.find_any_order(id);
    		
    		if (data != null)
			{
				return data;
			}
    	}
    	return new Object[3];
    }
    
    public static Object[][] display_all_groups()
    {
		Collection<Group> myset = customer_groups.values();
		Group[] values= myset.toArray(new Group[0]);
    	Object[][] all_groups = new Object[customer_groups.size()][5];
    	int i = 0;
    	for (Group group : values)
    	{
	    		all_groups[i] = group.give_array();
	    		i++;
    	}
    	return all_groups;
    }
    
}
